﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using VerifyArgs;

namespace DZParallelLib.Utility
{
    static class VerifyArgsHelpers
    {
        private static class CheckAllFilesExist<T> where T : class
        {
            public static readonly Func<Arguments<T>, Arguments<T>> Verifier =
                VerifyArgs.Codegen.VerifierFactory.Create<T, IEnumerable<FileInfo>>(
                t => t == typeof(IEnumerable<FileInfo>),
                items => !items.All(f => f.Exists),
                (n, _) => new ArgumentException(string.Format("Not all files in parameter '{0}' exist.", n))
                );
        }

        /// <summary>
        /// Check to see if all files in an array exist or not.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="args"></param>
        /// <returns></returns>
        public static Arguments<T> AllFilesExist<T>(this Arguments<T> args) where T : class
        {
            return CheckAllFilesExist<T>.Verifier(args);
        }

        /// <summary>
        /// Check to see if a single file exists.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        private static class CheckFileExist<T> where T : class
        {
            public static readonly Func<Arguments<T>, Arguments<T>> Verifier =
                VerifyArgs.Codegen.VerifierFactory.Create<T, FileInfo>(
                t => t == typeof(FileInfo),
                f => !f.Exists,
                (n, f) => new ArgumentException(string.Format("Argument '{0}' shoudl exist as a file, but file '{1}' does not exist.", n, f.FullName))
                );
        }

        /// <summary>
        /// Check to see if a single FileInfo exists.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="args"></param>
        /// <returns></returns>
        public static Arguments<T> Exists<T>(this Arguments<T> args) where T : class
        {
            return CheckFileExist<T>.Verifier(args);
        }
    }
}
